package com.glsc.ngateway.common.base.repo.mysql.gateway.ldap;

import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.LdapEhrDept;
import com.glsc.ngateway.common.api.ldap.dto.LdapEhrDeptSearchDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lium
 * @date 2022/5/9
 */
public interface LdapEhrDeptRepo extends JpaRepository<LdapEhrDept, Integer>, JpaSpecificationExecutor<LdapEhrDept> {

    @Transactional
    @Modifying
    @Query(value = "truncate table t_ldap_ehr_dept", nativeQuery = true)
    Integer truncateTableEhrDept();

    List<LdapEhrDept> findAllByOaDeptIdIn(List<Integer> oaIds);

    @Query(value = "select * from t_ldap_ehr_dept t " +
            "where 1=1 " +
            "and (:#{#param.aTitle} is null or t.atitle = :#{#param.aTitle}) " +
            "and (coalesce(:#{#param.oaDeptIdList}, null) is null or t.oa_dept_id in (:#{#param.oaDeptIdList})) " +
            "and (coalesce(:#{#param.dTitleList}, null) is null or t.dtitle in (:#{#param.dTitleList})) " +
            "", nativeQuery = true)
    List<LdapEhrDept> query(@Param("param") LdapEhrDeptSearchDto param);
}